package cn.ffcs.uec.insurance.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Div;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.portal.PortalException;
import cn.ffcs.raptornuke.portal.SystemException;
import cn.ffcs.uec.insurance.component.bean.UecBmclassListboxBean;
import cn.ffcs.uec.insurance.constant.UecBmConstants;
import cn.ffcs.uec.insurance.model.UecBmclass;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import lombok.Getter;
import lombok.Setter;


@Controller
@Scope("prototype")
public class UecBmclassListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -2212731640371254189L;

	/**
	 * 对应页面上的bean
	 */
	private UecBmclassListboxBean bean = new UecBmclassListboxBean();
	/**
	 * 
	 */
	private UecBmclass uecBmclass;
	/**
	 * 查询对象
	 */
	private UecBmclass qryUecBmclass;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecBmclassListboxExt() {
		Executions.createComponents(
				UecBmConstants.UEC_BMCLASS_EXT_LISTBOX_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		PubUtil.disableComponent(this.bean.getRows());
		//bean.getBirthDate().setDisabled(true);
		bean.getConditionValue().setReadonly(false);
		List<NodeVo> nodeVoList = new ArrayList<NodeVo>();
		NodeVo nodeVo = new NodeVo();
		nodeVo.setName("险种名称");
		nodeVo.setId("1");
		
		NodeVo nodeVo2 = new NodeVo();
		nodeVo2.setName("险种编码");
		nodeVo2.setId("2");
		
		nodeVoList.add(nodeVo);
		nodeVoList.add(nodeVo2);
		
		ListboxUtils.rendererForEdit(bean.getCondition(), nodeVoList);
		this.setButtonValid(true, false,false);
	}

	/**
	 * 查询按钮
	 * 
	 * @throws Exception
	 */
	public void onQuery() throws Exception {
		
		clearVariable();
		PubUtil.clearComponent(this.bean.getBasePanel());
		if (!StrUtil.isNullOrEmpty(bean.getCondition().getSelectedItem().getValue()) 
				&& !StrUtil.isNullOrEmpty(bean.getConditionValue().getValue())) {
			Listbox listbox = bean.getCondition();
			String listboxStr = (String) listbox.getSelectedItem().getValue();
			int listboxId = Integer.parseInt(listboxStr);
			qryUecBmclass = UecBmclass.newInstance();
			switch (listboxId) {
			case 1:
				qryUecBmclass.setName(bean.getConditionValue().getValue());
				break;
			case 2:
				qryUecBmclass.setCode(bean.getConditionValue().getValue());
				break;
			default:
				break;
			}
		}
		BmclassList();
		
	}
	
	/**
	 * 险种列表
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */
	public void BmclassList() throws SuspendNotAllowedException, Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("qryUecBmclass", qryUecBmclass);
		Window window = (Window) Executions.createComponents(
				UecBmConstants.UEC_BMCLASS_LIST_ZUL, this, map);
		window.doModal();
		window.addEventListener("onOK", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getData() != null) {
					qryUecBmclass = (UecBmclass) event.getData();
					uecBmclass = qryUecBmclass;
					PubUtil.fillBeanFromPo(uecBmclass, bean);
					/*if (!StrUtil.isEmpty(uecBmclass.getBirthDate())) {
						Date birth = DateUtil.strToDate(uecBmclass.getBirthDate());
						bean.getBirthDate().setValue(birth);;
					}*/
					//setUecStaffButtonValid(true,true,true);
					groupDataTranslationDisplay();
				}
			}
		});
	}
	
	/**
	 * 集团数据翻译展示,并向其他组件跑出事件
	 */
	public void groupDataTranslationDisplay() {
		if (null != uecBmclass) {
			
			if (null != uecBmclass.getPkBmClass()) {//判断是否为集团数据
				this.setButtonValid(true, false, false);
			}else {
				this.setButtonValid(true, true, true);
			}
			if (uecBmclass.getStopflag() != null) {
				String stopflag = uecBmclass.getStopflag();
				if ("Y".equals(stopflag)) {
					bean.getStopflag().setValue("是");
				} else if ("N".equals(stopflag)) {
					bean.getStopflag().setValue("否");
				} else {
					bean.getStopflag().setValue("");
				}
			}
			if (uecBmclass.getEnableorgbase() != null) {
				String enableorgbase = uecBmclass.getEnableorgbase();
				if ("Y".equals(enableorgbase)) {
					bean.getEnableorgbase().setValue("是");
				} else if ("N".equals(enableorgbase)) {
					bean.getEnableorgbase().setValue("否");
				} else {
					bean.getEnableorgbase().setValue("");
				}
			}
			if (uecBmclass.getEditdatasource() != null) {
				String editdatasource = uecBmclass.getEditdatasource();
				if ("Y".equals(editdatasource)) {
					bean.getEditdatasource().setValue("是");
				} else if ("N".equals(editdatasource)) {
					bean.getEditdatasource().setValue("否");
				} else {
					bean.getEditdatasource().setValue("");
				}
			}
			if (uecBmclass.getAllowadditem() != null) {
				String allowadditem = uecBmclass.getAllowadditem();
				if ("Y".equals(allowadditem)) {
					bean.getAllowadditem().setValue("是");
				} else if ("N".equals(allowadditem)) {
					bean.getAllowadditem().setValue("否");
				} else {
					bean.getAllowadditem().setValue("");
				}
			}
			if (uecBmclass.getDr() != null) {
				Long isDr = uecBmclass.getDr();
				if (1 == isDr) {
					bean.getDr().setValue("是");
				} else if (0 == isDr) {
					bean.getDr().setValue("否");
				} else {
					bean.getDr().setValue("");
				}
			}
		}
		// 重要！！
		Events.postEvent(UecBmConstants.ON_UEC_BMCLASS_QUERY, this,
				uecBmclass);
	}
	/**
	 * 重置
	 * 
	 * @throws Exception
	 */
	public void onReset() throws Exception {
		clearVariable();
		bean.getConditionValue().setValue(null);
		PubUtil.clearComponent(this);
		Events.postEvent(UecBmConstants.ON_CLEAN_TAB_UEC_BMCLASS_QUERY, this,
				null);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qryUecBmclass = null;
		uecBmclass = null;
	}

	/**
	 * Add
	 * 
	 * @throws PortalException
	 * @throws SystemException
	 */
	public void onAdd() throws PortalException, SystemException {
		openEditWin("add");
	}
	
	public void onEdit() throws PortalException, SystemException {
		openEditWin("edit");
	}
	
	public void onDel() throws PortalException, SystemException {
		try {

			Messagebox.show("您确定要删除吗？", "提示信息", Messagebox.OK
					| Messagebox.CANCEL, Messagebox.INFORMATION,
					new EventListener() {
						public void onEvent(Event event) throws Exception {
							Integer result = (Integer) event.getData();
							if (result == Messagebox.OK) {
								qryUecBmclass.removeOnly();
								setButtonValid(true, false, false);
								onReset();
							}
						}
					});
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	public void setButtonValid( boolean canAdd,boolean canEdit,boolean canDel) {
		bean.getAddBmclassButton().setDisabled(!canAdd);
		bean.getEditBmclassButton().setDisabled(!canEdit);
		bean.getDelBmclassButton().setDisabled(!canDel);
	}
	/**
	 * openUacStaffInfoWin
	 * 
	 * @param opType
	 */
	private void openEditWin(String opType) {
		Map<String, Object> arg = new HashMap<String, Object>();
		arg.put("opType", opType);
		if (opType.equals("edit")
				|| opType.equals("view")) {
			arg.put("id", qryUecBmclass.getId());
			arg.put("uecBmclass", qryUecBmclass);
		}
		Window win = (Window) Executions.createComponents(UecBmConstants.UEC_BMCLASS_EDIT_ZUL, this, arg);
		try {
			win.doModal();
			win.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						qryUecBmclass = (UecBmclass) event.getData();
						uecBmclass = qryUecBmclass;
						PubUtil.fillBeanFromPo(uecBmclass, bean);
						/*if (!StrUtil.isEmpty(uecBmclass.getBirthDate())) {
							Date birth = DateUtil.strToDate(uecBmclass.getBirthDate());
							bean.getBirthDate().setValue(birth);;
						}*/
						//setUecStaffButtonValid(true, true, true);
						groupDataTranslationDisplay();
					}
				}
			});
		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
